package com.sams.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sams.entity.Baseinfo;
import com.sams.entity.BaseinfoExample;
import com.sams.entity.Notice;
import com.sams.entity.NoticeExample;
import com.sams.entity.School;
import com.sams.entity.User;
import com.sams.entity.NoticeExample;
import com.sams.entity.NoticeExample.Criteria;
import com.sams.entity.NoticeShowinfo;
import com.sams.entity.NoticeShowinfoExample;
import com.sams.mapper.BaseinfoMapper;
import com.sams.mapper.NoticeMapper;
import com.sams.mapper.NoticeShowinfoMapper;
import com.sams.service.NoticeService;
@Service
public class NoticeServiceImpl implements NoticeService{

	@Autowired
	private NoticeMapper mapper;
	@Autowired
	private NoticeShowinfoMapper noticeShowInfoMapper;
	@Autowired
	private BaseinfoMapper baseinfoMapper;
	public int add(Notice notice) {
		// TODO Auto-generated method stub
		return mapper.insert(notice);
	}

	public int delete(int id) {
		 //先删除文件对象  
		// 删除用户浏览 的记录 
		
		return mapper.deleteByPrimaryKey(id);
	}

	public Notice findById(int id) {
		Notice n = mapper.selectByPrimaryKey(id);
		return n;
	}

	public PageInfo<Notice> list(int currentPage,String name) {
		// 1 定义当前页  和 一页显示的行数 
		  PageHelper.startPage(currentPage, 20);  
		 
		
		  NoticeExample example = new NoticeExample();
		 Criteria createCriteria = example.createCriteria();
		 
		 if(name!=null && !name.equals("")) {
			 createCriteria.andNameLike("%"+name+"%");
		 }
		 // 2  调用查询语句
		List<Notice> list =   mapper.selectByExampleWithBLOBs(example);
		PageInfo<Notice> pageInfo = new PageInfo<Notice>(list);
		 
		return pageInfo;
	}

	public List<Notice> getList() {
		// 1 定义当前页  和 一页显示的行数 
		  PageHelper.startPage(1, 5);  
		  NoticeExample example = new NoticeExample();
			 
			 example.setOrderByClause(" createdate desc");
		 // 2  调用查询语句
		List<Notice> list =   mapper.selectByExample(example);
		PageInfo<Notice> pageInfo = new PageInfo<Notice>(list);
		
		return pageInfo.getList();
	}
	/**
	 * 添加详情浏览的记录
	 */
	public int addNoticeShow(int noticeId, User user) {
		// 先查询该详情是否被用户浏览     根据用户id+ 公告id
		NoticeShowinfoExample example = new NoticeShowinfoExample();
		com.sams.entity.NoticeShowinfoExample.Criteria  cc= 
			example.createCriteria();
			 cc.andNoticeIdEqualTo(noticeId);
			 cc.andUserIdEqualTo(user.getId());
			 
		List<NoticeShowinfo> list = noticeShowInfoMapper.selectByExample(example);
		if(list.size()==0) {
			 NoticeShowinfo noticeshow = new NoticeShowinfo();
			 noticeshow.setNoticeId(noticeId);
			 noticeshow.setRoleId(user.getRoleid());
			 noticeshow.setUserId(user.getId());
			 noticeshow.setXxid(user.getXxid());
			 noticeshow.setZid(user.getZid());
			 return noticeShowInfoMapper.insert(noticeshow);
		}
		return 0;
		
	}
	/**
	 * 根据用户id ， 公告id 查询 已阅人员
	 */
	public List<NoticeShowinfo> listShowUser(int noticeId, User user) {
		NoticeShowinfo query = new NoticeShowinfo();
		query.setNoticeId(noticeId);
		if(user.getRoleid()==3 || user.getRoleid()==4) {
			// 查询级别为2的用户阅读
			query.setRoleId(2);
		}else if(user.getRoleid()==2) { // 镇账号 查看它自己所管辖的学校 
			query.setRoleId(1); // 查询自己管辖的学校
			List<Integer> xxids = new ArrayList<Integer>();
			//查询某乡镇 对应的 学校 
			BaseinfoExample example = new BaseinfoExample();
			com.sams.entity.BaseinfoExample.Criteria cc = example.createCriteria();
			cc.andpIdEqualTo(user.getZid()); // 用户的镇id  查询它管辖的额学校 
			
			List<Baseinfo> list = baseinfoMapper.selectByExample(example);
			for(Baseinfo b : list) {
				xxids.add(b.getId());   //学校ids
			}
			query.setXxids(xxids);
			
		}else if(user.getRoleid()==1) { //查询自己阅读
			query.setUserId(user.getId());
		}
		
		return noticeShowInfoMapper.selectUserName(query);
	}

	public List<NoticeShowinfo> selectSchoolNameByXZId(Integer noticeId, Integer userId) {
		// TODO Auto-generated method stub
		NoticeShowinfo info =  new NoticeShowinfo();
		info.setNoticeId(noticeId);
		info.setUserId(userId);
		return noticeShowInfoMapper.selectSchoolNameByXZId(info);
	}

	
	
}
